// subPages/mine/module/module.js
const app = getApp()
var util =  require('../../../utils/healthUtil.js');
var json =  require('../../../data/healthJson.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moduleList: json.moduleList,
    editorTxt: '编辑',
    isEditor: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getQX()
  },

  //获取数据
  getQX:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/get_qx', //接口地址
      data: {
        userid: app.globalData.userId
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        // if(!res.data.is_show&&!res.data.is_head){
        //   that.setData({
        //     useList: json.moduleList
        //   })
        // }
        that.getShowList(res.data.is_show);
        that.getHeadList(res.data.is_head);
      },
      fail: function (res) {
        util.showModal('提示','读取栏目列表失败')
      }
    })
  },

  //处理使用的数据
  getShowList:function(showList){
    var that = this
    if(showList.length!=0){
      let list = []
      let moduleList = that.data.moduleList
      for(var i in showList){
        switch (showList[i]) {
          case '1':
            list.push(moduleList[0])
            break;
          case '2':
            list.push(moduleList[1])
            break;
          case '3':
            list.push(moduleList[2])
            break;
          case '4':
            list.push(moduleList[3])
            break;
          // case '5':
          //   list.push(moduleList[4])
          //   break;
        }
      } 
      that.setData({
        useList: list
      })
    }else{
      that.setData({
        useList: []
      })
    }
  },

  //处理未使用使用的数据
  getHeadList:function(headList){
    var that = this
    if(headList.length!=0){
      let list = []
      let moduleList = that.data.moduleList
      for(var i in headList){
        switch (headList[i]) {
          case '1':
            list.push(moduleList[0])
            break;
          case '2':
            list.push(moduleList[1])
            break;
          case '3':
            list.push(moduleList[2])
            break;
          case '4':
            list.push(moduleList[3])
            break;
          // case '5':
          //   list.push(moduleList[4])
          //   break;
        }
      } 
      that.setData({
        unusedList: list
      })
    }else{
      that.setData({
        unusedList: []
      })
    }
  },

  //编辑按钮
  editor:function(e){
    var that = this
    if(that.data.isEditor){
      that.setData({
        editorTxt: '编辑',
        isEditor: false
      })
    }else{
      that.setData({
        editorTxt: '完成',
        isEditor: true
      })
    }
  },

  //增删按钮
  addDelete:function(e){
    var that = this
    var type = e.currentTarget.dataset.type
    var item = e.currentTarget.dataset.item
    var index = e.currentTarget.dataset.index    
    var useList = that.data.useList
    var unusedList = that.data.unusedList
    if(type == 'deltel'){
      //删除
      useList.splice(index,1);
      unusedList.push(item)
    }else{
      //增加
      unusedList.splice(index,1);
      useList.push(item)
    }
    that.setData({
      useList: useList.sort(util.compare('index')),
      unusedList: unusedList.sort(util.compare('index'))
    })
  },

  //判断模块
  judgmentModule:function(){
    var that = this
    var useList = that.data.useList
    var unusedList = that.data.unusedList
    var check = []
    var uncheck = []
    for(var i in useList){
      check.push(useList[i].index)
    }
    for(var i in unusedList){
      uncheck.push(unusedList[i].index)
    }
    that.setData({
      check: check,
      uncheck: uncheck
    })
  },

  //保存
  saveBtn:function(){
    var that = this
    that.judgmentModule()
    wx.request({
      url: app.globalData.apiUrl +'/save_qx', //接口地址
      data: {
        userid: app.globalData.userId,
        is_show: that.data.check,
        is_head: that.data.uncheck,
        is_qx: that.data.uncheck.length==0?0:1
      },
      method: 'post',
      header: {
          'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          util.showToast('保存成功','success')
          if(that.data.isEditor){
            that.setData({
              isEditor: false
            })
          }
          setTimeout(function(){
            wx.navigateBack({
              delta: 1, // 回退前 delta(默认为1) 页面
            })
          },500)
        }else{
          util.showModal('提示','保存模块失败')
        }
      },
      fail: function (res) {
        util.showModal('提示','保存模块失败')
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: 'PAPN体重管理',
      path: 'pages/index/index',
      success: function(res) {
        // res.shareTickets 
      },
      fail: function(res) {
          console.log(res)
      }
    }
  }
})